Accès XML avec Linq Talon Achille Greg 1963 Petitpas Prudence Maurice Maréchal 1957 Titeuf Zep 1992 s = " ..... "; xml = XElement.Parse(s); Retrouver les noms des personnages var listePers = from p in xml.Elements("Personnage") select p; foreach (var el in listePers) s += el.Element("Nom").Value + " - "; Dim listePers = From p In xml.Elements("Personnage") Select p For Each el In listePers s += el.Element("Nom").ToString() & " – " Next Retrouver les prénoms des personnages var o = el.Element("Prenom"); if (o == null) ..... .. la balise n’existe pas .. else ..... .. la balise existe .. Détecter si une balise contient une ou plusieurs balises var o = el.Element("Pere"); if (o.HasElements) ..... .. une ou plusieurs balises enfants .. else ..... .. pas de balise enfant pour cet élément .. if (o.HasElements) foreach (var x in o.Elements()) ..... .. nom de la balise dans x.Name .. Retrouver les attributs d’une balise if (el.Element("Pere").HasAttributes) ..... var attVN = el.Element("Pere").Attribute("VN"); if (attVN != null) ..... .. l’attribut existe bien .. Dim listePers = From p In xml.Elements("Personnage") Select p For Each el In listePers If el.Element("Pere").Attribute("VN") Is Nothing Then s += el.Element("Pere").Value & " – " Else : s += el.Element("Pere").Value & " (" _ & el.Element("Pere").Attribute("VN").Value & ")" & " – " End If Next Amélioration du select var listeNoms = from p in xml.Elements("Personnage") select p.Element("Nom").Value; foreach (var s in listeNoms) ..... .. nom dans s.Value .. var listePrénoms = from p in xml.Elements("Personnage") select p.Element("Prenom"); foreach (var s in listePrénoms) if (s != null) ..... .. prénom dans s.Value .. else ..... .. pas de prénom pour ce personnage .. Convertir le résultat d’une recherche en un tableau ou une liste var listeNoms = from p in xml.Elements("Personnage") select p.Element("Nom").Value; List liste = listeNoms.ToList(); var listeNoms = from p in xml.Elements("Personnage") select p.Element("Nom").Value; string[] ts = listeNoms.ToArray(); // nom du deuxième personnage dans ts[1] Dim listeNoms = From p In xml.Elements("Personnage") _ Select p.Element("Nom").Value Dim ts As String() = listeNoms.ToArray() ' nom du deuxième personnage dans ts(1) Création d’objets d’une classe à partir de balises public class Pers { public string Nom { get; set; } public string Prénom { get; set; } public int AnnéeCréation { get; set; } } var listePers = from p in xml.Elements("Personnage") select new Pers { Nom = (string)p.Element("Nom"), Prénom = (string)p.Element("Prenom"), AnnéeCréation = (int)p.Element("Creation") }; var listePers = from p in xml.Elements("Personnage") select new Pers { Nom = (string)p.Element("Nom"), Prénom = (string)p.Element("Prenom"), AnnéeCréation = p.Element("Creation")!=null ? (int)p.Element("Creation") : -1 }; Les contraintes et les tris var listePers = from p in xml.Elements("Personnage") where (int)p.Element("Creation") > 1960 orderby (int)p.Element("Creation") ascending select new Pers { Nom = (string)p.Element("Nom"), AnnéeCréation = (int)p.Element("Creation") }; int N = 1960; var listePers = from p in xml.Elements("Personnage") where (int)p.Element("Creation") > N orderby (int)p.Element("Creation") ascending select new Pers { Nom = (string)p.Element("Nom"), AnnéeCréation = (int)p.Element("Creation") }; Dim N As Integer = 1960 Dim listePers = From p In xml.Elements("Personnage") _ where CInt(Fix(p.Element("Creation"))) > N _ orderby (Integer)p.Element("Creation") ascending _ select New Pers With _ { _ .Nom = CStr(p.Element("Nom")), _ .AnnéeCréation = CInt(Fix(p.Element("Creation")))_ }